package com.caigou.service.impl;

import com.caigou.bean.cms.entity.SurveyFormStatistics;
import com.caigou.mapper.cms.SurveyStatisticsMapper;
import com.caigou.service.SurveyStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class SurveyStatisticsServiceImpl implements SurveyStatisticsService {

    @Resource
    private SurveyStatisticsMapper surveyStatisticsMapper;

    @Override
    public int saveSurveyFormStatistics(SurveyFormStatistics formStatistics) {
        int result = surveyStatisticsMapper.loadSurveyFormStatistics(formStatistics.getFormId());
        if (result == 0){
            // 保存数据
            int saveResult = surveyStatisticsMapper.saveSurveyFormStatistics(formStatistics);
            log.info("保存表单统计数据，formId={}, result={}",formStatistics.getFormId(),saveResult);
        } else {
            // 更新数据
            int updateResult = surveyStatisticsMapper.updateSurveyFormStatistics(formStatistics);
            log.info("更新表单统计数据，formId={}, result={}",formStatistics.getFormId(),updateResult);
        }
        return 1;
    }

    @Override
    public int updateSurveyFormStatistics(SurveyFormStatistics formStatistics) {
        int result = surveyStatisticsMapper.loadSurveyFormStatistics(formStatistics.getFormId());
        if (result == 0){
            // 保存数据
            int saveResult = surveyStatisticsMapper.saveSurveyFormStatistics(formStatistics);
            log.info("保存表单统计数据，formId={}, result={}",formStatistics.getFormId(),saveResult);
        } else {
            // 更新数据
            int updateResult = surveyStatisticsMapper.updateSurveyFormStatistics(formStatistics);
            log.info("更新表单统计数据，formId={}, result={}",formStatistics.getFormId(),updateResult);
        }
        return 1;
    }

    @Override
    public Integer loadCountByForm(String formId, String startTime, String endTime) {
        return surveyStatisticsMapper.loadCountByForm(formId,startTime,endTime);
    }

    @Override
    public Integer loadCountByQuestion(Integer questionId, String startTime, String endTime) {
        return surveyStatisticsMapper.loadCountByQuestion(questionId,startTime,endTime);
    }

    @Override
    public Integer loadCountByOption(Integer questionId, Integer optionId, String startTime, String endTime) {
        return surveyStatisticsMapper.loadCountByOption(questionId,optionId,startTime,endTime);
    }
}
